<?php

namespace App\Console\Commands;

use App\Http\Logic\Admin\NotesLogic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use PhpOffice\PhpSpreadsheet\Writer\Csv;

class ExpResume extends Command
{
    /**
     * The name and signature of the console command.
     * @var string
     */
    protected $signature = 'command:expResume';

    /**
     * The console command description.
     * @var string
     */
    protected $description = '手动导出简历';

    /**
     * Create a new command instance.
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @return int
     */
    public function handle()
    {
        $key_head = [
            "姓名",
            "手机号",
            "出生日期",
            "学历",
            "工作年限",
            "期望岗位",
            "期望城市",
            "简历标题",
            "简历创建时间",
        ];

        $data = NotesLogic::getNoteList([], 0, 0, "exp");

//        var_dump($data);
//        die;
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setTitle('sheet1');

        foreach ($key_head as $key => $value) {
            $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
        }

        $row = 2;
        foreach ($data as $item) {
            $sheet->setCellValue('A' . $row, $item->user_name);
            $sheet->setCellValue('B' . $row, $item->mobile);
            $sheet->setCellValue('C' . $row, date("Y-m-d H:i:s", $item->birth));
            $sheet->setCellValue('D' . $row, $item->educational_info);
            $sheet->setCellValue('E' . $row, $item->work_year);
            $sheet->setCellValue('F' . $row, $item->position_category_name);
            $sheet->setCellValue('G' . $row, $item->work_city_name);
            $sheet->setCellValue('H' . $row, $item->att_name ?? "");
            $sheet->setCellValue('I' . $row, date("Y-m-d H:i:s", $item->created_at));
            $row++;
        }
        date_default_timezone_set('Asia/Shanghai');
        $writer = new Csv($spreadsheet);

        $date     = date('Y-m-d');
        $filePath = 'resume/doc/' . $date . '/';
        $fileName = date('H-i-s') . ".csv";

        if (!is_dir(storage_path($filePath))) {
            File::makeDirectory(storage_path($filePath), 0777, true);
        }

        // 临时流
        $writer->save(storage_path($filePath) . $fileName);

        // 将导出的文件移动到指定的文件夹
        //        rename(storage_path($filePathTmp) . $fileName, storage_path($filePath) . $fileName);

        // 返回下载链接或其他响应
        //        return response()->download(storage_path($filePath) . $fileName)->deleteFileAfterSend(true);

    }
}
